package com.jdone.compus.client;

import com.jdone.compus.config.IocConfig;
import com.jdone.compus.dto.ApiResponse;
import com.jdone.compus.utils.BlowSignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 海尔 IoC 客户端
 * 用于绑定、解绑、设备操作
 */
@Component
public class HaileIocClient {
    private static final Logger logger = LoggerFactory.getLogger(HaileIocClient.class);

    private static final String BIND_PATH    = "/paas/bind";
    private static final String UNBIND_PATH  = "/paas/unbind";
    private static final String OPERATE_PATH = "/paas/switch";

    private final IocConfig config;
    private final RestTemplate rest;

    /**
     * 构造函数
     * @param config        IoC 配置类
     * @param restTemplate  Spring 注入的 RestTemplate（可带代理、超时等配置）
     */
    public HaileIocClient(IocConfig config, RestTemplate restTemplate) {
        this.config = config;
        this.rest = restTemplate;
    }

    /**
     * 构造公共请求头
     */
    private HttpHeaders buildHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return headers;
    }

    /**
     * 统一 POST 调用
     */
    @SuppressWarnings("unchecked")
    private ApiResponse post(String path, Map<String, Object> bizParams) {
        String url = config.getBaseUrl() + path;
        Map<String, Object> signedParams = BlowSignUtil.buildParams(bizParams, config.getAppId(), config.getAppKey());


//        System.out.println(url);
//        System.out.println(signedParams);

        return rest.postForObject(url, new HttpEntity<>(signedParams, buildHeaders()), ApiResponse.class);
    }

    /**
     * 绑定设备
     */
    public ApiResponse bind(String imei,
                            String deviceNo,
                            String modelCode,
                            String deviceName,
                            String deviceLocation,
                            String deviceAddress) {
        Map<String, Object> biz = new HashMap<>(6);
        biz.put("imei", imei);
        biz.put("device_no", deviceNo);
        biz.put("model_code", modelCode);
        biz.put("device_name", deviceName);
        biz.put("device_location", deviceLocation);
        biz.put("device_address", deviceAddress);
        return post(BIND_PATH, biz);
    }

    /**
     * 解绑设备
     */
    public ApiResponse unbind(String imei) {
        Map<String, Object> biz = new HashMap<>(1);
        biz.put("imei", imei);
        return post(UNBIND_PATH, biz);
    }

    /**
     * 下发控制指令
     */
    public ApiResponse operate(String imei, String codeJson) {
        Map<String, Object> biz = new HashMap<>(2);
        biz.put("imei", imei);
        biz.put("code", codeJson);
        return post(OPERATE_PATH, biz);
    }
}
